(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.b.invoice.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','piInquiryService','$filter'];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService ,piInquiryService,$filter) {
		var ctrl = this,  params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.txnStatusOptions = getTxnStatus().data;
			ctrl.searchCriteria.assetId = params.assetId;
			ctrl.searchCriteria.ivfrRequestId = params.ivfrRequestId;
			ctrl.search();
			
		};
		function getTxnStatus() {
			var options = {
				"data" : [{
					"label" : "label.txnstatus.c",
					"value" : "C"
				}, {
					"label" : "label.txnstatus.w",
					"value" : "W"    
				}, {
					"label" : "label.txnstatus.l",
					"value" : "L"    
				}, {
					"label" : "label.txnstatus.j",
					"value" : "J"    
				}, {
					"label" : "label.txnstatus.z",
					"value" : "Z"    
				}, {
					"label" : "label.txnstatus.f",
					"value" : "F"    
				}, {
					"label" : "label.txnstatus.r",
					"value" : "R"    
				}, {
					"label" : "label.txnstatus.x",
					"value" : "X"    
				}, {
					"label" : "label.txnstatus.p",
					"value" : "P"    
				}, {
					"label" : "label.txnstatus.m",
					"value" : "M"    
				}, {
					"label" : "label.txnstatus.o",
					"value" : "O"    
				} ]
			};
			return options;
		}
		ctrl.journal = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('apf.b.piInquiry.invoice.invJournal.list', {parentId : $filter('encode')(ctrl.selectedRows[0].invoiceId)});
		};
		ctrl.creditSettle = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('apf.b.invoice.invCreditSettle.list', {invoiceId : $filter('encode')(ctrl.selectedRows[0].invoiceId)});
		};
		
		ctrl.viewInvoiceDetails = function(rowEntity){
			var params = {};
			params.invoice = rowEntity;
			modalService.openCustomDialogs({
				'title': 'title.invoice.detail',
				'dialogsModule' : 'apf.invoice.detail',
				'params' : params,
				'btnCallback' : {
					'ok' : function() {
						
					}
				}
			});
		}
		
		ctrl.payment = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('apf.b.piInquiry.invoice.payment.list', {invoiceId : $filter('encode')(ctrl.selectedRows[0].invoiceId)});
		};
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			ctrl.searchCriteria.piId = params.piId;
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);










